package 接口测试.xml测试;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Date;

public class TEST {
    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException {
        test();
    }


    public static void test() throws ParserConfigurationException, IOException, SAXException {
        String xmlReturn = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><MDEML templateVersion=\"1.0\"><header><result><success>0</success><message>失败</message></result><createDate>20250825090611</createDate><dataType>D301</dataType><sourceCode ref=\"630000\">青海省级平台</sourceCode><targetCode ref=\"00\">国家平台</targetCode><sourceType ref=\"1\">省级平台上传</sourceType></header><body><bodyItem itemId=\"8a6881ab9540c07b019541482f5708d2\"><data><D301-02>低于长度最小值</D301-02></data></bodyItem></body></MDEML>";

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        org.w3c.dom.Document doc = builder.parse(new InputSource(new StringReader(xmlReturn)));

        // 提取 success 和 message
        NodeList nodeList = doc.getElementsByTagName("success");
        String success = nodeList.item(0).getTextContent();

        NodeList nodeMessageList = doc.getElementsByTagName("message");
        String message = nodeMessageList.item(0).getTextContent();

        System.out.println("Success: " + success);
        System.out.println("Message: " + message);

        // 正确提取 data 节点的 XML 内容
        NodeList nodeListError = doc.getElementsByTagName("bodyItem");
        StringBuilder stringBuilderError = new StringBuilder();
        for (int i = 0; i < nodeListError.getLength(); i++) {
            Node node = nodeListError.item(i);
            String nodeXml = nodeToString(node); // 转为 XML 字符串
            stringBuilderError.append(nodeXml);
        }
        System.out.println("Data XML: " + stringBuilderError.toString());
    }

    // 辅助方法：将 Node 转为格式化的 XML 字符串
    private static String nodeToString(Node node) {
        try {
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            StringWriter writer = new StringWriter();
            transformer.transform(new DOMSource(node), new StreamResult(writer));
            return writer.getBuffer().toString();
        } catch (TransformerException e) {
            throw new RuntimeException("Error converting node to string", e);
        }
    }
}
